<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="网络编程,">





  <link rel="alternate" href="/atom.xml" title="恋恋风辰的个人博客" type="application/atom+xml">






<meta name="description" content="libevent框架之前有做过分析，这次是谈谈如何将libevent搭建在vs工作环境下，并且编写一个demo进行测试。测试过程中会再一次带大家分析消息是怎么传递的。我的libevent版本libevent-2.0.22-stable，用对应的vs命令工具进入该目录">
<meta name="keywords" content="网络编程">
<meta property="og:type" content="article">
<meta property="og:title" content="windows环境libevent搭建和demo分析">
<meta property="og:url" content="http://www.limerence2017.com/2017/08/07/winlibevent/index.html">
<meta property="og:site_name" content="恋恋风辰的个人博客">
<meta property="og:description" content="libevent框架之前有做过分析，这次是谈谈如何将libevent搭建在vs工作环境下，并且编写一个demo进行测试。测试过程中会再一次带大家分析消息是怎么传递的。我的libevent版本libevent-2.0.22-stable，用对应的vs命令工具进入该目录">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/1.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/2.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/3.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/4.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/5.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/6.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/7.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/8.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/9.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/10.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/11.png">
<meta property="og:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/12.png">
<meta property="og:updated_time" content="2020-06-22T09:30:59.112Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="windows环境libevent搭建和demo分析">
<meta name="twitter:description" content="libevent框架之前有做过分析，这次是谈谈如何将libevent搭建在vs工作环境下，并且编写一个demo进行测试。测试过程中会再一次带大家分析消息是怎么传递的。我的libevent版本libevent-2.0.22-stable，用对应的vs命令工具进入该目录">
<meta name="twitter:image" content="http://www.limerence2017.com/2017/08/07/winlibevent/winlibevent/1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://www.limerence2017.com/2017/08/07/winlibevent/">





  <title>windows环境libevent搭建和demo分析 | 恋恋风辰的个人博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">恋恋风辰的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            Archives
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br>
            
            Tags
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            Search
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="Searching..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://www.limerence2017.com/2017/08/07/winlibevent/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="恋恋风辰">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="恋恋风辰的个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">windows环境libevent搭建和demo分析</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2017-08-07T10:53:56+08:00">
                2017-08-07
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="/categories/netprogram/" itemprop="url" rel="index">
                    <span itemprop="name">网络编程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><code>libevent框架</code>之前有做过分析，这次是谈谈如何将libevent搭建在<code>vs工作环境下</code>，并且编写一个demo进行测试。测试过程中会再一次带大家分析消息是怎么传递的。我的libevent版本<code>libevent-2.0.22-stable</code>，用对应的vs命令工具进入该目录<br><img src="winlibevent/1.png" alt="1.png"><br><a id="more"></a><br>我的是<code>Visual Studio 2008版本</code>的Command Prompt<br><img src="winlibevent/2.png" alt="2.png"><br>执行成功后在libevent目录下生成三个lib<br><img src="winlibevent/3.png" alt="3.png"><br>之后用vs创建控制台项目<br><img src="winlibevent/4.png" alt="4.png"><br>生成成功后在项目目录里创建Include和Lib两个文件夹<br><img src="winlibevent/5.png" alt="5.png"><br>分别进入libevent这两个目录里边<br><img src="winlibevent/6.png" alt="6.png"><br>将内部的所有文件拷贝到Include文件夹里，event内容重复可以合并我们项目目录Include文件夹下的内容为<br><img src="winlibevent/7.png" alt="7.png"><br>将libevent库中的三个lib拷贝到项目的Lib文件夹里<br>下一步配置项目属性，完成编译<br><code>1、配置头文件包含路径，C++/General/Additional Include Directories  配置为相对路径的Include（因配置的路径不同而异）</code><br><img src="winlibevent/8.png" alt="8.png"><br><code>2、配置代码生成</code><br>C/C++ /Code Generation RuntimeLibrary 设置为MTD,因为库的生成是按照这个MTD模式生成的，所以要匹配<br><img src="winlibevent/9.png" alt="9.png"><br><code>3、配置 C/C++ /Advanced/Compile As Compile as C++ Code (/TP)</code><br>（因为我的工程用到C++的函数所以配置这个）网上有人推荐配置成TC的也可以，自己根据项目需要<br><img src="winlibevent/10.png" alt="10.png"><br><code>4、配置库目录</code><br>Linker/General/Additional Library Directories   ..\Lib(根据自己的Lib文件夹和项目相对位置填写)<br><img src="winlibevent/11.png" alt="11.png"><br>5<code>配置 Linker\Input\AdditionalLibraries</code>   ws2_32.lib;wsock32.lib;libevent.lib;libevent_core.lib;libevent_extras.lib;<br><img src="winlibevent/12.png" alt="12.png"><br>6 <code>配置忽略项，可以不配置</code> 输入\忽略特定默认库 libc.lib;msvcrt.lib;libcd.lib;libcmtd.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries)<br>生成lib后，不带调试信息，无法单步进函数里，所以要修改脚本：<code>Makefile.nmake第二行CFLAGS=$(CFLAGS) /Od /W3 /wd4996 /nologo /Zi</code><br>到此为止项目配置好了，我们来写相关的demo代码</p>
<p>主函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> **argv)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event_base</span> *<span class="title">base</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> *<span class="title">listener</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event</span> *<span class="title">signal_event</span>;</span></span><br><span class="line"></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">sockaddr_in</span> <span class="title">sin</span>;</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> WIN32</span></span><br><span class="line">    WSADATA wsa_data;</span><br><span class="line">    WSAStartup(<span class="number">0x0201</span>, &amp;wsa_data);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">    <span class="comment">//创建event_base</span></span><br><span class="line">    base = event_base_new();</span><br><span class="line">    <span class="keyword">if</span> (!base) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Could not initialize libevent!\n"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">memset</span>(&amp;<span class="built_in">sin</span>, <span class="number">0</span>, <span class="keyword">sizeof</span>(<span class="built_in">sin</span>));</span><br><span class="line">    <span class="built_in">sin</span>.sin_family = AF_INET;</span><br><span class="line">    <span class="built_in">sin</span>.sin_port = htons(PORT);</span><br><span class="line">    <span class="built_in">sin</span>.sin_addr.s_addr = inet_addr(<span class="string">"192.168.1.99"</span>);</span><br><span class="line">    <span class="comment">//std::string ipstr = inet_ntoa(sin.sin_addr);</span></span><br><span class="line">    <span class="comment">//std::cout &lt;&lt; ipstr.c_str();</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">//基于eventbase 生成listen描述符并绑定</span></span><br><span class="line">    <span class="comment">//设置了listener_cb回调函数，当有新的连接登录的时候</span></span><br><span class="line">    <span class="comment">//触发listener_cb</span></span><br><span class="line">    listener = evconnlistener_new_bind(base, listener_cb, (<span class="keyword">void</span> *)base,</span><br><span class="line">        LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, <span class="number">-1</span>,</span><br><span class="line">        (struct sockaddr*)&amp;<span class="built_in">sin</span>,</span><br><span class="line">        <span class="keyword">sizeof</span>(<span class="built_in">sin</span>));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!listener) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Could not create a listener!\n"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//设置终端信号，当程序收到SIGINT后调用signal_cb</span></span><br><span class="line">    signal_event = evsignal_new(base, SIGINT, signal_cb, (<span class="keyword">void</span> *)base);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!signal_event || event_add(signal_event, <span class="literal">NULL</span>)&lt;<span class="number">0</span>) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Could not create/add a signal event!\n"</span>);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//event_base消息派发</span></span><br><span class="line">    event_base_dispatch(base);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//释放生成的evconnlistener</span></span><br><span class="line">    evconnlistener_free(listener);</span><br><span class="line">    <span class="comment">//释放生成的信号事件</span></span><br><span class="line">    event_free(signal_event);</span><br><span class="line">    <span class="comment">//释放event_base</span></span><br><span class="line">    event_base_free(base);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"done\n"</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>listener回调函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">listener_cb</span><span class="params">(struct evconnlistener *listener, <span class="keyword">evutil_socket_t</span> fd,</span></span></span><br><span class="line"><span class="function"><span class="params">    struct sockaddr *sa, <span class="keyword">int</span> socklen, <span class="keyword">void</span> *user_data)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event_base</span> *<span class="title">base</span> = (<span class="title">struct</span> <span class="title">event_base</span> *)<span class="title">user_data</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bufferevent</span> *<span class="title">bev</span>;</span></span><br><span class="line">    <span class="comment">//生成一个bufferevent，用于读或者写</span></span><br><span class="line">    bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);</span><br><span class="line">    <span class="keyword">if</span> (!bev) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">fprintf</span>(<span class="built_in">stderr</span>, <span class="string">"Error constructing bufferevent!"</span>);</span><br><span class="line">        event_base_loopbreak(base);</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//设置了写回调函数和事件的回调函数</span></span><br><span class="line">    bufferevent_setcb(bev, <span class="literal">NULL</span>, conn_writecb, conn_eventcb, <span class="literal">NULL</span>);</span><br><span class="line">    <span class="comment">//bufferevent设置写事件回调</span></span><br><span class="line">    bufferevent_enable(bev, EV_WRITE);</span><br><span class="line">    <span class="comment">//bufferevent关闭读事件回调</span></span><br><span class="line">    bufferevent_disable(bev, EV_READ);</span><br><span class="line">    <span class="comment">//将MESSAGE字符串拷贝到outbuffer里</span></span><br><span class="line">    bufferevent_write(bev, MESSAGE, <span class="built_in">strlen</span>(MESSAGE));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>一些基本参数</p>
<p>static const char MESSAGE[] = “Hello, NewConnection!\n”;</p>
<p>static const int PORT = 9995;<br>bufferevent的写回调函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">conn_writecb</span><span class="params">(struct bufferevent *bev, <span class="keyword">void</span> *user_data)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="comment">//取出bufferevent 的output数据</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evbuffer</span> *<span class="title">output</span> = <span class="title">bufferevent_get_output</span>(<span class="title">bev</span>);</span></span><br><span class="line">    <span class="comment">//长度为0，那么写完毕，释放空间</span></span><br><span class="line">    <span class="keyword">if</span> (evbuffer_get_length(output) == <span class="number">0</span>) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"flushed answer\n"</span>);</span><br><span class="line">        bufferevent_free(bev);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>bufferevent的事件回调函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//仅仅作为事件回调函数，写自己想要做的功能就行</span></span><br><span class="line"><span class="comment">//最后记得释放buffevent空间</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">conn_eventcb</span><span class="params">(struct bufferevent *bev, <span class="keyword">short</span> events, <span class="keyword">void</span> *user_data)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (events &amp; BEV_EVENT_EOF) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"Connection closed.\n"</span>);</span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (events &amp; BEV_EVENT_ERROR) </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"Got an error on the connection: %s\n"</span>,</span><br><span class="line">            strerror(errno));<span class="comment">/*XXX win32*/</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/* None of the other events can happen here, since we haven't enabled</span></span><br><span class="line"><span class="comment">     * timeouts */</span></span><br><span class="line">    bufferevent_free(bev);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>信号终止函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//程序捕捉到信号后就让baseloop终止</span></span><br><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">signal_cb</span><span class="params">(<span class="keyword">evutil_socket_t</span> sig, <span class="keyword">short</span> events, <span class="keyword">void</span> *user_data)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event_base</span> *<span class="title">base</span> = (<span class="title">struct</span> <span class="title">event_base</span> *)<span class="title">user_data</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">timeval</span> <span class="title">delay</span> = &#123;</span> <span class="number">2</span>, <span class="number">0</span> &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">"Caught an interrupt signal; exiting cleanly in two seconds.\n"</span>);</span><br><span class="line"></span><br><span class="line">    event_base_loopexit(base, &amp;delay);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>整个demo完成了。</p>
<p>下面分析下libevent如何做的消息传递和回调注册函数从main函数中的evconnlistener_new_bind入手</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> *</span></span><br><span class="line"><span class="class"><span class="title">evconnlistener_new_bind</span>(<span class="title">struct</span> <span class="title">event_base</span> *<span class="title">base</span>, <span class="title">evconnlistener_cb</span> <span class="title">cb</span>,</span></span><br><span class="line"><span class="class">    <span class="title">void</span> *<span class="title">ptr</span>, <span class="title">unsigned</span> <span class="title">flags</span>, <span class="title">int</span> <span class="title">backlog</span>, <span class="title">const</span> <span class="title">struct</span> <span class="title">sockaddr</span> *<span class="title">sa</span>,</span></span><br><span class="line"><span class="class">    <span class="title">int</span> <span class="title">socklen</span>)</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> *<span class="title">listener</span>;</span></span><br><span class="line">    <span class="keyword">evutil_socket_t</span> fd;</span><br><span class="line">    <span class="keyword">int</span> on = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">int</span> family = sa ? sa-&gt;sa_family : AF_UNSPEC;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (backlog == <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    fd = socket(family, SOCK_STREAM, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (fd == <span class="number">-1</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (evutil_make_socket_nonblocking(fd) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        evutil_closesocket(fd);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (flags &amp; LEV_OPT_CLOSE_ON_EXEC) &#123;</span><br><span class="line">        <span class="keyword">if</span> (evutil_make_socket_closeonexec(fd) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            evutil_closesocket(fd);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (<span class="keyword">void</span>*)&amp;on, <span class="keyword">sizeof</span>(on))&lt;<span class="number">0</span>) &#123;</span><br><span class="line">        evutil_closesocket(fd);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (flags &amp; LEV_OPT_REUSEABLE) &#123;</span><br><span class="line">        <span class="keyword">if</span> (evutil_make_listen_socket_reuseable(fd) &lt; <span class="number">0</span>) &#123;</span><br><span class="line">            evutil_closesocket(fd);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (sa) &#123;</span><br><span class="line">        <span class="keyword">if</span> (bind(fd, sa, socklen)&lt;<span class="number">0</span>) &#123;</span><br><span class="line">            evutil_closesocket(fd);</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//cb = listener_cb,  ptr = struct event_base *base;</span></span><br><span class="line">    listener = evconnlistener_new(base, cb, ptr, flags, backlog, fd);</span><br><span class="line">    <span class="keyword">if</span> (!listener) &#123;</span><br><span class="line">        evutil_closesocket(fd);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> listener;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>evconnlistener_new_bind 完成了socket生成和绑定，并且内部调用evconnlistener_new</p>
<p>生成了evconnlistener* listener,将listener和socket绑定在一起。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> *</span></span><br><span class="line"><span class="class"><span class="title">evconnlistener_new</span>(<span class="title">struct</span> <span class="title">event_base</span> *<span class="title">base</span>,</span></span><br><span class="line"><span class="class">    <span class="title">evconnlistener_cb</span> <span class="title">cb</span>, <span class="title">void</span> *<span class="title">ptr</span>, <span class="title">unsigned</span> <span class="title">flags</span>, <span class="title">int</span> <span class="title">backlog</span>,</span></span><br><span class="line"><span class="class">    <span class="title">evutil_socket_t</span> <span class="title">fd</span>)</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener_event</span> *<span class="title">lev</span>;</span></span><br><span class="line">    <span class="keyword">if</span> (backlog &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (listen(fd, backlog) &lt; <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (backlog &lt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (listen(fd, <span class="number">128</span>) &lt; <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    &#125;</span><br><span class="line">       <span class="comment">//开辟evconnlistener_event大小区域</span></span><br><span class="line">    lev = mm_calloc(<span class="number">1</span>, <span class="keyword">sizeof</span>(struct evconnlistener_event));</span><br><span class="line">    <span class="keyword">if</span> (!lev)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">NULL</span>;</span><br><span class="line">    <span class="comment">//lev -&gt; base 表示  evconnlistener </span></span><br><span class="line">    <span class="comment">//evconnlistener     evconnlistener_ops 基本回调参数和回调函数结构体赋值</span></span><br><span class="line">    lev-&gt;base.ops = &amp;evconnlistener_event_ops;</span><br><span class="line">    <span class="comment">//evconnlistener_cb 设置为listener_cb</span></span><br><span class="line">    lev-&gt;base.cb = cb;</span><br><span class="line">    <span class="comment">//ptr表示event_base 指针</span></span><br><span class="line">    lev-&gt;base.user_data = ptr;</span><br><span class="line">    lev-&gt;base.flags = flags;</span><br><span class="line">    lev-&gt;base.refcnt = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (flags &amp; LEV_OPT_THREADSAFE) &#123;</span><br><span class="line">        EVTHREAD_ALLOC_LOCK(lev-&gt;base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  lev   is evconnlistener_event       </span></span><br><span class="line">    <span class="comment">//lev-&gt;listener is event</span></span><br><span class="line">    <span class="comment">//为lev-&gt;listener设置读回调函数和读关注事件，仅进行设置并没加入event队列</span></span><br><span class="line">    event_assign(&amp;lev-&gt;listener, base, fd, EV_READ|EV_PERSIST,</span><br><span class="line">        listener_read_cb, lev);</span><br><span class="line">    <span class="comment">//实际调用了event_add将事件加入event队列</span></span><br><span class="line">    evconnlistener_enable(&amp;lev-&gt;base);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> &amp;lev-&gt;base;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">lev = mm_calloc(<span class="number">1</span>, <span class="keyword">sizeof</span>(struct evconnlistener_event));开辟了一个</span><br><span class="line"></span><br><span class="line">evconnlistener_event* 空间，evconnlistener_event类型如下</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener_event</span> &#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> <span class="title">base</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event</span> <span class="title">listener</span>;</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>该结构包含一个evconnlistener和event事件结构体</p>
<p>evconnlistener结构如下</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> &#123;</span></span><br><span class="line">    <span class="comment">//listener基本操作封装成一个结构体</span></span><br><span class="line">    <span class="comment">//结构体包含操作的函数指针</span></span><br><span class="line">    <span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener_ops</span> *<span class="title">ops</span>;</span></span><br><span class="line">    <span class="keyword">void</span> *lock;</span><br><span class="line">    <span class="comment">//listener回调函数，有新的连接到来会触发</span></span><br><span class="line">    evconnlistener_cb cb;</span><br><span class="line">    <span class="comment">//listener有错误会触发这个函数</span></span><br><span class="line">    evconnlistener_errorcb errorcb;</span><br><span class="line">    <span class="comment">//存储一些回调函数用到的参数</span></span><br><span class="line">    <span class="keyword">void</span> *user_data;</span><br><span class="line">    <span class="keyword">unsigned</span> flags;</span><br><span class="line">    <span class="keyword">short</span> refcnt;</span><br><span class="line">    <span class="keyword">unsigned</span> enabled : <span class="number">1</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener_ops</span> &#123;</span></span><br><span class="line">　　　　<span class="keyword">int</span> (*enable)(struct evconnlistener *);</span><br><span class="line">　　　　<span class="keyword">int</span> (*disable)(struct evconnlistener *);</span><br><span class="line">　　　　<span class="keyword">void</span> (*destroy)(struct evconnlistener *);</span><br><span class="line">　　　　<span class="keyword">void</span> (*shutdown)(struct evconnlistener *);</span><br><span class="line">　　　　<span class="keyword">evutil_socket_t</span> (*getfd)(struct evconnlistener *);</span><br><span class="line">　　　　<span class="class"><span class="keyword">struct</span> <span class="title">event_base</span> *(*<span class="title">getbase</span>)(<span class="title">struct</span> <span class="title">evconnlistener</span> *);</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>lev-&gt;base.ops = &evconnlistener_event_ops;这句就是对这个结构体指针赋值，evconnlistener_event_ops是一个实例化的结构体对象，里面包含定义好的操作函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener_ops</span> <span class="title">evconnlistener_event_ops</span> = &#123;</span></span><br><span class="line">    event_listener_enable,</span><br><span class="line">    event_listener_disable,</span><br><span class="line">    event_listener_destroy,</span><br><span class="line">    <span class="literal">NULL</span>, <span class="comment">/* shutdown */</span></span><br><span class="line">    event_listener_getfd,</span><br><span class="line">    event_listener_getbase</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>对lev-&gt;base其余参数的赋值就不一一解释了。接下来看一下event_assign函数内部实现</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (!base)</span><br><span class="line">        base = current_base;</span><br><span class="line"></span><br><span class="line">    _event_debug_assert_not_added(ev);</span><br><span class="line">       <span class="comment">//属于哪个event_base</span></span><br><span class="line">    ev-&gt;ev_base = base;</span><br><span class="line">       <span class="comment">//事件回调函数</span></span><br><span class="line">    ev-&gt;ev_callback = callback;</span><br><span class="line">    <span class="comment">//回调函数的参数</span></span><br><span class="line">    ev-&gt;ev_arg = arg;</span><br><span class="line">    <span class="comment">//event关注哪个fd</span></span><br><span class="line">    ev-&gt;ev_fd = fd;</span><br><span class="line">    <span class="comment">//event事件类型</span></span><br><span class="line">    ev-&gt;ev_events = events;</span><br><span class="line">    ev-&gt;ev_res = <span class="number">0</span>;</span><br><span class="line">    ev-&gt;ev_flags = EVLIST_INIT;</span><br><span class="line">    <span class="comment">//被调用过几次</span></span><br><span class="line">    ev-&gt;ev_ncalls = <span class="number">0</span>;</span><br><span class="line">    ev-&gt;ev_pncalls = <span class="literal">NULL</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (events &amp; EV_SIGNAL) &#123;</span><br><span class="line">        <span class="keyword">if</span> ((events &amp; (EV_READ|EV_WRITE)) != <span class="number">0</span>) &#123;</span><br><span class="line">            event_warnx(<span class="string">"%s: EV_SIGNAL is not compatible with "</span></span><br><span class="line">                <span class="string">"EV_READ or EV_WRITE"</span>, __func__);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ev-&gt;ev_closure = EV_CLOSURE_SIGNAL;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (events &amp; EV_PERSIST) &#123;</span><br><span class="line">            evutil_timerclear(&amp;ev-&gt;ev_io_timeout);</span><br><span class="line">            ev-&gt;ev_closure = EV_CLOSURE_PERSIST;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            ev-&gt;ev_closure = EV_CLOSURE_NONE;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    min_heap_elem_init(ev);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (base != <span class="literal">NULL</span>) &#123;</span><br><span class="line">        <span class="comment">/* by default, we put new events into the middle priority */</span></span><br><span class="line">        <span class="comment">//优先级的设置</span></span><br><span class="line">        ev-&gt;ev_pri = base-&gt;nactivequeues / <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    _event_debug_note_setup(ev);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>event_assign</code>内部实现可以看出该函数仅仅对<code>event</code>的属性进行设置</p>
<p><code>event_assign</code>主要对event设置了<code>listener_read_cb</code>回调函数，这是</p>
<p>很重要的一个细节，我们看下listener_read_cb内部实现</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">void</span></span><br><span class="line">listener_read_cb(<span class="keyword">evutil_socket_t</span> fd, <span class="keyword">short</span> what, <span class="keyword">void</span> *p)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener</span> *<span class="title">lev</span> = <span class="title">p</span>;</span></span><br><span class="line">    <span class="keyword">int</span> err;</span><br><span class="line">    evconnlistener_cb cb;</span><br><span class="line">    evconnlistener_errorcb errorcb;</span><br><span class="line">    <span class="keyword">void</span> *user_data;</span><br><span class="line">    LOCK(lev);</span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> <span class="title">sockaddr_storage</span> <span class="title">ss</span>;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> WIN32</span></span><br><span class="line">        <span class="keyword">int</span> socklen = <span class="keyword">sizeof</span>(ss);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">else</span></span></span><br><span class="line">        <span class="keyword">socklen_t</span> socklen = <span class="keyword">sizeof</span>(ss);</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">        <span class="comment">//调用accept生成新的fd</span></span><br><span class="line">        <span class="keyword">evutil_socket_t</span> new_fd = accept(fd, (struct sockaddr*)&amp;ss, &amp;socklen);</span><br><span class="line">        <span class="keyword">if</span> (new_fd &lt; <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">if</span> (socklen == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">/* This can happen with some older linux kernels in</span></span><br><span class="line"><span class="comment">             * response to nmap. */</span></span><br><span class="line">            evutil_closesocket(new_fd);</span><br><span class="line">            <span class="keyword">continue</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//设置非阻塞</span></span><br><span class="line">        <span class="keyword">if</span> (!(lev-&gt;flags &amp; LEV_OPT_LEAVE_SOCKETS_BLOCKING))</span><br><span class="line">            evutil_make_socket_nonblocking(new_fd);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (lev-&gt;cb == <span class="literal">NULL</span>) &#123;</span><br><span class="line">            evutil_closesocket(new_fd);</span><br><span class="line">            UNLOCK(lev);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        ++lev-&gt;refcnt;</span><br><span class="line">        <span class="comment">//cb 就 是  listener_cb</span></span><br><span class="line">        cb = lev-&gt;cb;</span><br><span class="line">        user_data = lev-&gt;user_data;</span><br><span class="line">        UNLOCK(lev);</span><br><span class="line">        <span class="comment">//触发了listener_cb</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//完成了eventbuffer注册写和事件函数  </span></span><br><span class="line">        cb(lev, new_fd, (struct sockaddr*)&amp;ss, (<span class="keyword">int</span>)socklen,</span><br><span class="line">            user_data);</span><br><span class="line">        </span><br><span class="line">        LOCK(lev);</span><br><span class="line">        <span class="keyword">if</span> (lev-&gt;refcnt == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> freed = listener_decref_and_unlock(lev);</span><br><span class="line">            EVUTIL_ASSERT(freed);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        --lev-&gt;refcnt;</span><br><span class="line">    &#125;</span><br><span class="line">   。。。。。。。。。</span><br><span class="line">&#125;</span><br><span class="line">``` </span><br><span class="line">在evconnlistener_new中 </span><br><span class="line"><span class="comment">//evconnlistener_cb 设置为listener_cb</span></span><br><span class="line">lev-&gt;base.cb = cb;</span><br><span class="line"></span><br><span class="line">`lev-&gt;base`就是`evconnlistener`对象</span><br><span class="line"></span><br><span class="line">`listener_read_cb`内部回调用绑定在`evconnlistener`的`listener_cb`。</span><br><span class="line"></span><br><span class="line">记得之前我所说的绑定么？`evconnlistener_new`这个函数里生成的`lev`，</span><br><span class="line"></span><br><span class="line">之后对lev，这里的lev就是 evconnlistener_event对象，lev-&gt;listener是</span><br><span class="line"></span><br><span class="line">event对象，通过调用`event_assign(&amp;lev-&gt;listener, base, fd, EV_READ|EV_PERSIST,</span><br><span class="line">listener_read_cb, lev);`</span><br><span class="line"></span><br><span class="line">lev-&gt;listener绑定的就是`listener_read_cb`。也就是是说</span><br><span class="line"></span><br><span class="line">`listener_read_cb`调用后，从而调用了绑定在`evconnlistener`的`listener_cb`。</span><br><span class="line"></span><br><span class="line">那么我们只要知道lev-&gt;listener（event对象）的读事件是如何派发的就可以梳理此</span><br><span class="line"></span><br><span class="line">流程了。</span><br><span class="line"></span><br><span class="line">之前我们梳理过`event_dispatch`里进行的事件派发，调用不同模型的dispatch，</span><br><span class="line"></span><br><span class="line">稍后再梳理一遍。因为调用`event_assign`仅仅对event设置了属性，还没有加到</span><br><span class="line"></span><br><span class="line">事件队列里。</span><br><span class="line"></span><br><span class="line">在`evconnlistener_new`函数里调用完`event_assign`，之后调用的是</span><br><span class="line"></span><br><span class="line">`evconnlistener_enable`，evconnlistener_enable这个函数完成了事件</span><br><span class="line"></span><br><span class="line">添加到事件队列的功能。</span><br><span class="line"></span><br><span class="line">``` cpp</span><br><span class="line"><span class="keyword">int</span></span><br><span class="line">evconnlistener_enable(struct evconnlistener *lev)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> r;</span><br><span class="line">    LOCK(lev);</span><br><span class="line">    lev-&gt;enabled = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (lev-&gt;cb)</span><br><span class="line">        <span class="comment">//调用evconnlistener 的ops的enable函数</span></span><br><span class="line">        <span class="comment">//lev-&gt;ops 此时指向evconnlistener_event_ops</span></span><br><span class="line">        <span class="comment">//enable函数为  event_listener_enable</span></span><br><span class="line">        r = lev-&gt;ops-&gt;enable(lev);</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        r = <span class="number">0</span>;</span><br><span class="line">    UNLOCK(lev);</span><br><span class="line">    <span class="keyword">return</span> r;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面有evconnlistener_event_ops结构体，那几个函数也列出来了。</p>
<p>我们看下event_listener_enable函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">int</span></span><br><span class="line">event_listener_enable(struct evconnlistener *lev)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//通过evconnlistener* 找到evconnlistener_event *</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evconnlistener_event</span> *<span class="title">lev_e</span> =</span></span><br><span class="line"><span class="class">        <span class="title">EVUTIL_UPCAST</span>(<span class="title">lev</span>, <span class="title">struct</span> <span class="title">evconnlistener_event</span>, <span class="title">base</span>);</span></span><br><span class="line">    <span class="comment">//将</span></span><br><span class="line">    <span class="keyword">return</span> event_add(&amp;lev_e-&gt;listener, <span class="literal">NULL</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>里面调用了event_add</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//添加事件操作</span></span><br><span class="line"><span class="keyword">int</span></span><br><span class="line">event_add(struct event *ev, <span class="keyword">const</span> struct timeval *tv)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">int</span> res;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (EVUTIL_FAILURE_CHECK(!ev-&gt;ev_base)) &#123;</span><br><span class="line">        event_warnx(<span class="string">"%s: event has no event_base set."</span>, __func__);</span><br><span class="line">        <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    EVBASE_ACQUIRE_LOCK(ev-&gt;ev_base, th_base_lock);</span><br><span class="line">    <span class="comment">//添加事件核心函数</span></span><br><span class="line">    res = event_add_internal(ev, tv, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">    EVBASE_RELEASE_LOCK(ev-&gt;ev_base, th_base_lock);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (res);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>对于<code>event_add</code>内部判断是否枷锁，进行加锁，然后调用</p>
<p><code>event_add_internal完成事件添加</code></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">int</span></span><br><span class="line">event_add_internal(struct event *ev, <span class="keyword">const</span> struct timeval *tv,</span><br><span class="line">    <span class="keyword">int</span> tv_is_absolute)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event_base</span> *<span class="title">base</span> = <span class="title">ev</span>-&gt;<span class="title">ev_base</span>;</span></span><br><span class="line">    <span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">int</span> notify = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">   </span><br><span class="line">    <span class="comment">//根据不同的事件类型将事件放到evmap里，调用不同模型的add函数</span></span><br><span class="line">    <span class="comment">//将事件按照EV_READ或者EV_WRITE或者EV_SIGNAL放入evmap事件队列里</span></span><br><span class="line">    <span class="comment">//将ev按照EVLIST_INSERTED放入用户的事件队列里</span></span><br><span class="line">    <span class="keyword">if</span> ((ev-&gt;ev_events &amp; (EV_READ|EV_WRITE|EV_SIGNAL)) &amp;&amp;</span><br><span class="line">        !(ev-&gt;ev_flags &amp; (EVLIST_INSERTED|EVLIST_ACTIVE))) &#123;</span><br><span class="line">        <span class="keyword">if</span> (ev-&gt;ev_events &amp; (EV_READ|EV_WRITE))</span><br><span class="line">            <span class="comment">//将事件按照读写  IO方式加入evmap里，并且调用不同网络模型的add完成事件添加</span></span><br><span class="line">            res = evmap_io_add(base, ev-&gt;ev_fd, ev);</span><br><span class="line">        <span class="keyword">else</span> <span class="keyword">if</span> (ev-&gt;ev_events &amp; EV_SIGNAL)</span><br><span class="line">            <span class="comment">//将事件按照信号方式添加</span></span><br><span class="line">            res = evmap_signal_add(base, (<span class="keyword">int</span>)ev-&gt;ev_fd, ev);</span><br><span class="line">        <span class="comment">//将事件插入轮询的事件队列里</span></span><br><span class="line">        <span class="keyword">if</span> (res != <span class="number">-1</span>)</span><br><span class="line">            event_queue_insert(base, ev, EVLIST_INSERTED);</span><br><span class="line">        <span class="keyword">if</span> (res == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="comment">/* evmap says we need to notify the main thread. */</span></span><br><span class="line">            notify = <span class="number">1</span>;</span><br><span class="line">            res = <span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于lev-&gt;listener（event）类型的事件是I/O 读事件，所以会进入evmap_io_add完成读事件的添加</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span></span><br><span class="line">evmap_io_add(struct event_base *base, <span class="keyword">evutil_socket_t</span> fd, struct event *ev)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">eventop</span> *<span class="title">evsel</span> = <span class="title">base</span>-&gt;<span class="title">evsel</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event_io_map</span> *<span class="title">io</span> = &amp;<span class="title">base</span>-&gt;<span class="title">io</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evmap_io</span> *<span class="title">ctx</span> = <span class="title">NULL</span>;</span></span><br><span class="line">    <span class="keyword">int</span> nread, nwrite, retval = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">short</span> res = <span class="number">0</span>, old = <span class="number">0</span>;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event</span> *<span class="title">old_ev</span>;</span></span><br><span class="line"></span><br><span class="line">    EVUTIL_ASSERT(fd == ev-&gt;ev_fd);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (fd &lt; <span class="number">0</span>)</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"><span class="comment">//windows情况下use a hashtable instead of an array</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> EVMAP_USE_HT</span></span><br><span class="line">    <span class="keyword">if</span> (fd &gt;= io-&gt;nentries) &#123;</span><br><span class="line">        <span class="keyword">if</span> (evmap_make_space(io, fd, <span class="keyword">sizeof</span>(struct evmap_io *)) == <span class="number">-1</span>)</span><br><span class="line">            <span class="keyword">return</span> (<span class="number">-1</span>);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//从io中找到下标为fd的结构体数据 evmap_io * 赋值给ctx</span></span><br><span class="line">    <span class="comment">//如果没有找到就调用evmap_io_init初始化</span></span><br><span class="line">    GET_IO_SLOT_AND_CTOR(ctx, io, fd, evmap_io, evmap_io_init,</span><br><span class="line">                         evsel-&gt;fdinfo_len);</span><br><span class="line"></span><br><span class="line">    nread = ctx-&gt;nread;</span><br><span class="line">    nwrite = ctx-&gt;nwrite;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (nread)</span><br><span class="line">        old |= EV_READ;</span><br><span class="line">    <span class="keyword">if</span> (nwrite)</span><br><span class="line">        old |= EV_WRITE;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (ev-&gt;ev_events &amp; EV_READ) &#123;</span><br><span class="line">        <span class="keyword">if</span> (++nread == <span class="number">1</span>)</span><br><span class="line">            res |= EV_READ;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (ev-&gt;ev_events &amp; EV_WRITE) &#123;</span><br><span class="line">        <span class="keyword">if</span> (++nwrite == <span class="number">1</span>)</span><br><span class="line">            res |= EV_WRITE;</span><br><span class="line">    &#125;</span><br><span class="line">   ......<span class="keyword">if</span> (res) &#123;</span><br><span class="line">        <span class="keyword">void</span> *extra = ((<span class="keyword">char</span>*)ctx) + <span class="keyword">sizeof</span>(struct evmap_io);</span><br><span class="line">        <span class="comment">/* XXX(niels): we cannot mix edge-triggered and</span></span><br><span class="line"><span class="comment">         * level-triggered, we should probably assert on</span></span><br><span class="line"><span class="comment">         * this. */</span></span><br><span class="line">         <span class="comment">//这里就是调用了不同模型的demultiplexer的添加操作</span></span><br><span class="line">         <span class="comment">//调用不同的网络模型add接口</span></span><br><span class="line">        <span class="keyword">if</span> (evsel-&gt;add(base, ev-&gt;ev_fd,</span><br><span class="line">            old, (ev-&gt;ev_events &amp; EV_ET) | res, extra) == <span class="number">-1</span>)</span><br><span class="line">            <span class="keyword">return</span> (<span class="number">-1</span>);</span><br><span class="line">        retval = <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    ctx-&gt;nread = (<span class="keyword">ev_uint16_t</span>) nread;</span><br><span class="line">    ctx-&gt;nwrite = (<span class="keyword">ev_uint16_t</span>) nwrite;</span><br><span class="line">    <span class="comment">//哈希表对应的event事件队列加入ev</span></span><br><span class="line">    TAILQ_INSERT_TAIL(&amp;ctx-&gt;events, ev, ev_io_next);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> (retval);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>该函数内部的一些函数就不展开，挺好理解的。到此为止我们了解了listen描述符的回调函数和读事件的绑定。</p>
<p>回到main函数，看下<code>event_base_dispatch就知道绑定在lev-&gt;listener（event）类型的读事件listener_read_cb</code></p>
<p>是如何派发的，进而在读事件里完成了evconnlistener的listener_cb的调用。<br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span></span><br><span class="line">event_base_dispatch(struct event_base *event_base)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">return</span> (event_base_loop(event_base, <span class="number">0</span>));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><code>int
event_base_loop(struct event_base *base, int flags)这个函数内部</code></p>
<p>我们只看关键代码</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span></span><br><span class="line">event_base_loop(struct event_base *base, <span class="keyword">int</span> flags)</span><br><span class="line">&#123;</span><br><span class="line">　　</span><br><span class="line">　　<span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">eventop</span> *<span class="title">evsel</span> = <span class="title">base</span>-&gt;<span class="title">evsel</span>;</span></span><br><span class="line">　　　　<span class="comment">//不同模型的派发函数</span></span><br><span class="line">　　　　<span class="comment">//evsel就是指向ops数组的某一种模型</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">　　res = evsel-&gt;dispatch(base, tv_p);</span><br><span class="line">   </span><br><span class="line">　　</span><br><span class="line">　　<span class="keyword">if</span> (N_ACTIVE_CALLBACKS(base)) &#123;</span><br><span class="line"></span><br><span class="line">　　　　<span class="comment">//处理激活队列中的事件</span></span><br><span class="line">　　　　<span class="keyword">int</span> n = event_process_active(base);</span><br><span class="line">　　　　<span class="keyword">if</span> ((flags &amp; EVLOOP_ONCE)</span><br><span class="line">　　　　　　&amp;&amp; N_ACTIVE_CALLBACKS(base) == <span class="number">0</span></span><br><span class="line">　　　　　　　　&amp;&amp; n != <span class="number">0</span>)</span><br><span class="line">　　　　　　done = <span class="number">1</span>;</span><br><span class="line">　　　　&#125; <span class="keyword">else</span> <span class="keyword">if</span> (flags &amp; EVLOOP_NONBLOCK)</span><br><span class="line">　　　　　　done = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>之前有说过evsel初始化和模型选择的代码，这里重新梳理下<code>const struct eventop *evsel</code>;</p>
<p><code>event_base_new或者event_init内部调用了event_base_new_with_config，</code></p>
<p><code>event_base_new_with_config函数调用了base-&gt;evsel = eventops[i]</code>;</p>
<p><code>eventops属主封装了几种模型结构体的指针</code></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">eventop</span> *<span class="title">eventops</span>[] = &#123;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> _EVENT_HAVE_EVENT_PORTS</span></span><br><span class="line">    &amp;evportops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> _EVENT_HAVE_WORKING_KQUEUE</span></span><br><span class="line">    &amp;kqops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> _EVENT_HAVE_EPOLL</span></span><br><span class="line">    &amp;epollops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> _EVENT_HAVE_DEVPOLL</span></span><br><span class="line">    &amp;devpollops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> _EVENT_HAVE_POLL</span></span><br><span class="line">    &amp;pollops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> _EVENT_HAVE_SELECT</span></span><br><span class="line">    &amp;selectops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> WIN32</span></span><br><span class="line">    &amp;win32ops,</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line">    <span class="literal">NULL</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>举个例子，看下<code>epollops</code></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">eventop</span> <span class="title">epollops</span> = &#123;</span></span><br><span class="line">    <span class="string">"epoll"</span>,</span><br><span class="line">    epoll_init,</span><br><span class="line">    epoll_nochangelist_add,</span><br><span class="line">    epoll_nochangelist_del,</span><br><span class="line">    epoll_dispatch,</span><br><span class="line">    epoll_dealloc,</span><br><span class="line">    <span class="number">1</span>, <span class="comment">/* need reinit */</span></span><br><span class="line">    EV_FEATURE_ET|EV_FEATURE_O1,</span><br><span class="line">    <span class="number">0</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p><code>eventop 类型</code><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">eventop</span> &#123;</span></span><br><span class="line">    <span class="comment">/** The name of this backend. */</span></span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">char</span> *name;</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">void</span> *(*init)(struct event_base *);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">int</span> (*add)(struct event_base *, <span class="keyword">evutil_socket_t</span> fd, <span class="keyword">short</span> old, <span class="keyword">short</span> events, <span class="keyword">void</span> *fdinfo);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> (*del)(struct event_base *, <span class="keyword">evutil_socket_t</span> fd, <span class="keyword">short</span> old, <span class="keyword">short</span> events, <span class="keyword">void</span> *fdinfo);</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">int</span> (*dispatch)(struct event_base *, struct timeval *);</span><br><span class="line">    <span class="comment">/** Function to clean up and free our data from the event_base. */</span></span><br><span class="line">    <span class="keyword">void</span> (*dealloc)(struct event_base *);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> need_reinit;</span><br><span class="line">   </span><br><span class="line">    <span class="keyword">enum</span> event_method_feature features;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">size_t</span> fdinfo_len;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></p>
<p><code>epollops是eventop类型的变量，实现了增加删除，初始化，销毁，派发等功能</code>。</p>
<p>所以<code>当模型选择epoll时,res = evsel-&gt;dispatch(base, tv_p)</code>;实际调用的是epoll的派发函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">int</span> <span class="title">epoll_dispatch</span><span class="params">(struct event_base *base, struct timeval *tv)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"></span><br><span class="line">　　<span class="class"><span class="keyword">struct</span> <span class="title">epollop</span> *<span class="title">epollop</span> = <span class="title">base</span>-&gt;<span class="title">evbase</span>;</span></span><br><span class="line">　　　　<span class="class"><span class="keyword">struct</span> <span class="title">epoll_event</span> *<span class="title">events</span> = <span class="title">epollop</span>-&gt;<span class="title">events</span>;</span></span><br><span class="line"></span><br><span class="line">　　res = epoll_wait(epollop-&gt;epfd, events, epollop-&gt;nevents, timeout);</span><br><span class="line"></span><br><span class="line">　　<span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; res; i++) &#123;</span><br><span class="line">　　　　　　<span class="keyword">int</span> what = events[i].events;</span><br><span class="line">　　　　　　<span class="keyword">short</span> ev = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">　　　　　　<span class="keyword">if</span> (what &amp; (EPOLLHUP|EPOLLERR)) &#123;</span><br><span class="line">　　　　　　　　ev = EV_READ | EV_WRITE;</span><br><span class="line">　　　　　　&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">　　　　　　　　　　<span class="keyword">if</span> (what &amp; EPOLLIN)</span><br><span class="line">　　　　　　　　　　　　ev |= EV_READ;</span><br><span class="line">　　　　　　　　　　<span class="keyword">if</span> (what &amp; EPOLLOUT)</span><br><span class="line">　　　　　　　　　　ev |= EV_WRITE;</span><br><span class="line">　　　　　　　　&#125;</span><br><span class="line"></span><br><span class="line">　　　　　　　　<span class="keyword">if</span> (!ev)</span><br><span class="line">　　　　　　　　　　<span class="keyword">continue</span>;</span><br><span class="line"></span><br><span class="line">　　　　　　　　<span class="comment">//更新evmap，并且将事件放入active队列</span></span><br><span class="line">　　　　　　　　evmap_io_active(base, events[i].data.fd, ev | EV_ET);</span><br><span class="line">　　　　　　&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>`evmap_io_active函数内部调用event_active_nolock，event_active_nolock中调用</p>
<p>event_queue_insert(base, ev, EVLIST_ACTIVE);负责将event放入激活队列里，</p>
<p>并且更新event在evmap中的 标记状态。`</p>
<p>到目前为止我们了解了事件派发的流程，<code>event_base_loop循环执行网络模型的dispatch，内核返回就绪事件，dispatch内部调用evmap_io_active将就绪事件放入激活队列里</code>。</p>
<p><code>在event_base_loop中调用event_process_active处理就绪队列中的event</code>。比如内核返回listen描述符读就绪事件，那么就会将listen的event放入就绪队列中，</p>
<p><code>在event_process_active处理event的读事件</code>，调用了之前绑定的listener_read_cb回调函数。</p>
<p>下面看下</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">int</span></span><br><span class="line">event_process_active(struct event_base *base)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">/* Caller must hold th_base_lock */</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">event_list</span> *<span class="title">activeq</span> = <span class="title">NULL</span>;</span></span><br><span class="line">    <span class="keyword">int</span> i, c = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">//循环处理就绪队列中的每一个就绪事件</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; base-&gt;nactivequeues; ++i) &#123;</span><br><span class="line">        <span class="keyword">if</span> (TAILQ_FIRST(&amp;base-&gt;activequeues[i]) != <span class="literal">NULL</span>) &#123;</span><br><span class="line">            base-&gt;event_running_priority = i;</span><br><span class="line">            activeq = &amp;base-&gt;activequeues[i];</span><br><span class="line">            c = event_process_active_single_queue(base, activeq);</span><br><span class="line">            <span class="keyword">if</span> (c &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                base-&gt;event_running_priority = <span class="number">-1</span>;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c &gt; <span class="number">0</span>)</span><br><span class="line">                <span class="keyword">break</span>; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//调用延时回调函数</span></span><br><span class="line">    event_process_deferred_callbacks(&amp;base-&gt;defer_queue,&amp;base-&gt;event_break);</span><br><span class="line">    base-&gt;event_running_priority = <span class="number">-1</span>;</span><br><span class="line">    <span class="keyword">return</span> c;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>循环调用了event_process_active_single_queue</code><br><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (ev-&gt;ev_closure) &#123;</span><br><span class="line">        <span class="keyword">case</span> EV_CLOSURE_SIGNAL:</span><br><span class="line">            event_signal_closure(base, ev);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> EV_CLOSURE_PERSIST:</span><br><span class="line">            event_persist_closure(base, ev);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">        <span class="keyword">case</span> EV_CLOSURE_NONE:</span><br><span class="line">            EVBASE_RELEASE_LOCK(base, th_base_lock);</span><br><span class="line">            (*ev-&gt;ev_callback)(</span><br><span class="line">                ev-&gt;ev_fd, ev-&gt;ev_res, ev-&gt;ev_arg);</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></p>
<p><code>(*ev-&gt;ev_callback)(ev-&gt;ev_fd, ev-&gt;ev_res, ev-&gt;ev_arg);就是调用绑定在event上的回调函数。</code>比如绑定在lev-&gt;listener（event）类型的读事件listener_read_cb;</p>
<p>从而调用了绑定在evconnlistener的listener_cb。</p>
<p>这样整个流程就跑通了。最上面有listener_cb函数的实现，整个消息传递流程不跟踪了，读者可以模仿上面的方式去跟踪消息。</p>
<p>这里简单表述下在bufferevent创建的时候调用了这个函数</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">bufferevent</span> *</span></span><br><span class="line"><span class="class"><span class="title">bufferevent_socket_new</span>(<span class="title">struct</span> <span class="title">event_base</span> *<span class="title">base</span>, <span class="title">evutil_socket_t</span> <span class="title">fd</span>,</span></span><br><span class="line"><span class="class">    <span class="title">int</span> <span class="title">options</span>)</span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bufferevent_private</span> *<span class="title">bufev_p</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">bufferevent</span> *<span class="title">bufev</span>;</span></span><br><span class="line"></span><br><span class="line">    ...</span><br><span class="line">　　　　<span class="comment">//设置bufferevent中 ev_read(event类型)回调函数</span></span><br><span class="line">　　　　event_assign(&amp;bufev-&gt;ev_read, bufev-&gt;ev_base, fd,</span><br><span class="line">　　　　EV_READ|EV_PERSIST, bufferevent_readcb, bufev);</span><br><span class="line">　　　　<span class="comment">//设置bufferevent中 ev_write(event类型)回调函数</span></span><br><span class="line">　　　　event_assign(&amp;bufev-&gt;ev_write, bufev-&gt;ev_base, fd,</span><br><span class="line">　　　　EV_WRITE|EV_PERSIST, bufferevent_writecb, bufev);</span><br><span class="line"></span><br><span class="line">　　　　<span class="comment">//为bufev-&gt;output(evbuffer类型)设置回调函数，回调函数内部将ev_write事件加入事件队列</span></span><br><span class="line">　　　　evbuffer_add_cb(bufev-&gt;output, bufferevent_socket_outbuf_cb, bufev);</span><br><span class="line"></span><br><span class="line">　　 ...</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> bufev;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>函数内部为ev_read和ev_write 设置默认的回调函数bufferevent_readcb和bufferevent_writecb。</code>接着为输出的 evbuffer绑定bufferevent_socket_outbuf_cb函数。</p>
<p>bufferevent_socket_outbuf_cb函数如果发现我们有可写的东西，并且没开始写，那么 将ev_</p>
<p>write事件加入event队列，跟上面的轮询一样，有可写就绪事件就会触发绑定在ev_write上的bufferevent-writecb函数。如果没有添加写的数据，就跳出函数。之后调用。由于这时处于bufferevent刚创建状态，那么说明没有数据写入bufferevent，所以这时是不会将ev_write加入event队列的。回到listener_cb函数。接着调用bufferevent_setcb 函数设置bufferevent的读，写，事件，</p>
<p>回调函数。</p>
<p>调用bufferevent_enable使写事件生效，</p>
<p>内部调用</p>
<p><code>bufev-&gt;be_ops-&gt;enable(bufev, impl_events)；</code></p>
<p>bufferevent注册好的回调函数如下</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="class"><span class="keyword">struct</span> <span class="title">bufferevent_ops</span> <span class="title">bufferevent_ops_socket</span> = &#123;</span></span><br><span class="line">    <span class="string">"socket"</span>,</span><br><span class="line">    evutil_offsetof(struct bufferevent_private, bev),</span><br><span class="line">    be_socket_enable,</span><br><span class="line">    be_socket_disable,</span><br><span class="line">    be_socket_destruct,</span><br><span class="line">    be_socket_adj_timeouts,</span><br><span class="line">    be_socket_flush,</span><br><span class="line">    be_socket_ctrl,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">int</span></span><br><span class="line">be_socket_enable(struct bufferevent *bufev, <span class="keyword">short</span> event)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">if</span> (event &amp; EV_READ) &#123;</span><br><span class="line">        <span class="keyword">if</span> (be_socket_add(&amp;bufev-&gt;ev_read,&amp;bufev-&gt;timeout_read) == <span class="number">-1</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (event &amp; EV_WRITE) &#123;</span><br><span class="line">        <span class="keyword">if</span> (be_socket_add(&amp;bufev-&gt;ev_write,&amp;bufev-&gt;timeout_write) == <span class="number">-1</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>eventbuffer读写事件加入到event队列里，此处为添加ev_write写事件，当写事件就绪，</p>
<p>轮询可以出发绑定的bufferevent_writecb回调函数。</p>
<p>当调用<code>bufferevent_writecb</code>这个函数时，我们把内部代码简化分析</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">void</span></span><br><span class="line">bufferevent_writecb(<span class="keyword">evutil_socket_t</span> fd, <span class="keyword">short</span> event, <span class="keyword">void</span> *arg)</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line"><span class="comment">//计算bufferevent能写的最大数量</span></span><br><span class="line">    atmost = _bufferevent_get_write_max(bufev_p);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (bufev_p-&gt;write_suspended)</span><br><span class="line">        <span class="keyword">goto</span> done;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (evbuffer_get_length(bufev-&gt;output)) &#123;</span><br><span class="line">        evbuffer_unfreeze(bufev-&gt;output, <span class="number">1</span>);</span><br><span class="line">        <span class="comment">//bufferevent调用写操作，将outbuffer中的内容发送出去</span></span><br><span class="line">        res = evbuffer_write_atmost(bufev-&gt;output, fd, atmost);</span><br><span class="line">        evbuffer_freeze(bufev-&gt;output, <span class="number">1</span>);</span><br><span class="line">        <span class="keyword">if</span> (res == <span class="number">-1</span>) &#123;</span><br><span class="line">            <span class="keyword">int</span> err = evutil_socket_geterror(fd);</span><br><span class="line">            <span class="keyword">if</span> (EVUTIL_ERR_RW_RETRIABLE(err))</span><br><span class="line">                <span class="keyword">goto</span> reschedule;</span><br><span class="line">            what |= BEV_EVENT_ERROR;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="comment">/* eof case</span></span><br><span class="line"><span class="comment">               XXXX Actually, a 0 on write doesn't indicate</span></span><br><span class="line"><span class="comment">               an EOF. An ECONNRESET might be more typical.</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">            what |= BEV_EVENT_EOF;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (res &lt;= <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">goto</span> error;</span><br><span class="line">        <span class="comment">//bufferevent减少发送的大小，留下未发送的，下次再发送</span></span><br><span class="line">        _bufferevent_decrement_write_buckets(bufev_p, res);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//计算是否将outbuf中的内容发送完，发完了就删除写事件</span></span><br><span class="line">    <span class="keyword">if</span> (evbuffer_get_length(bufev-&gt;output) == <span class="number">0</span>) &#123;</span><br><span class="line">        event_del(&amp;bufev-&gt;ev_write);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">     * Invoke the user callback if our buffer is drained or below the</span></span><br><span class="line"><span class="comment">     * low watermark.</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">     <span class="comment">//将buffer中的内容发完，或者低于low 水位，那么调用用户注册的写回调函数</span></span><br><span class="line">    <span class="keyword">if</span> ((res || !connected) &amp;&amp;</span><br><span class="line">        evbuffer_get_length(bufev-&gt;output) &lt;= bufev-&gt;wm_write.low) &#123;</span><br><span class="line">        _bufferevent_run_writecb(bufev);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><code>_bufferevent_run_writecb内部调用了bufev-&gt;writecb(bufev, bufev-&gt;cbarg);也就是说我们自己实现的</code></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> <span class="keyword">void</span></span><br><span class="line">conn_writecb(struct bufferevent *bev, <span class="keyword">void</span> *user_data)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="class"><span class="keyword">struct</span> <span class="title">evbuffer</span> *<span class="title">output</span> = <span class="title">bufferevent_get_output</span>(<span class="title">bev</span>);</span></span><br><span class="line">    <span class="keyword">if</span> (evbuffer_get_length(output) == <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">"flushed answer\n"</span>);</span><br><span class="line">        bufferevent_free(bev);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>到此为止整个bufferevent消息走向梳理出来。最后有一点需要陈述，在listener_cb中最后调用bufferevent_write(bev, MESSAGE, strlen(MESSAGE));</p>
<p>`其内部调用evbuffer_add，该函数内部evbuffer_invoke_callbacks(buf);会调用bufferevent_socket_outbuf_cb，进而调用</p>
<p>bufferevent_write。`</p>
<p><code>所以我认为是调用evbuffer_add向outbuf中添加数据后，调用了evbuffer_invoke_callbacks,触发bufferevent_write，
或者ev_write先检测到写就绪事件，然后调用buffervent_write.这两者先后并不清楚</code>。</p>
<p>整个流程就是这样，需要继续研究然后梳理。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/网络编程/" rel="tag"># 网络编程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/08/07/libevent1/" rel="next" title="libevent文档学习(一)多线程接口和使用">
                <i class="fa fa-chevron-left"></i> libevent文档学习(一)多线程接口和使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/08/07/libevlearn/" rel="prev" title="libevlearn">
                libevlearn <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">恋恋风辰</p>
              <p class="site-description motion-element" itemprop="description">重剑无锋，大巧不工</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">146</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">categories</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">tags</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/secondtonone1" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="https://www.zhihu.com/people/wang-wu-chen-32/activities" target="_blank" title="ZhiHu">
                    
                      <i class="fa fa-fw fa-ZhiHu"></i>ZhiHu</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">恋恋风辰</span>

  
  
 
    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
    <span class="post-meta-divider">|</span>
    <span id="busuanzi_container_site_uv">本站访客数<span id="busuanzi_value_site_uv"></span>人</span>
 
 
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.3</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
